package com.fiveflavors.suittar.gui.mainpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.io.IOException;
import java.net.URL;
import java.util.List;

import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.text.Document;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.StyleSheet;

import twitter4j.DirectMessage;
import twitter4j.User;
import twitter4j.UserWithStatus;

import com.fiveflavors.suittar.Suittar;
import com.fiveflavors.suittar.gui.SuittarHyperLinkListener;
import com.fiveflavors.suittar.gui.mainpanel.view.SuittarDirectMessageView;
import com.fiveflavors.suittar.gui.mainpanel.view.SuittarTimeLineView;
import com.fiveflavors.suittar.gui.mainpanel.view.SuittarTimeLineViewBean;
import com.fiveflavors.suittar.gui.mainpanel.view.SuittarTimeLineViewManager;
import com.fiveflavors.suittar.gui.mainpanel.view.SuittarUsersView;
import com.fiveflavors.suittar.resources.SuittarResourceManager;

public class SuittarMainPane extends JScrollPane {
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 5411402400611930599L;
	private URL aboutURL = null;
	private JEditorPane editor = null;

    public SuittarMainPane() {

    	editor = new JEditorPane();
		editor.setEditable(false);
		editor.setContentType("text/html");
		editor.setBackground(new Color(154,228,232));

		
		HTMLDocument doc = (HTMLDocument) editor.getDocument();
		StyleSheet ss = doc.getStyleSheet();
		Style body = ss.getStyleSheets()[0].getStyle("body");
		if (body != null) {
			body.addAttribute(StyleConstants.FontFamily, "SanSerif");
			body.addAttribute(StyleConstants.FontSize, "10pt");
		}
		Style link = ss.getStyleSheets()[0].getStyle("a");
		if (link != null) {
			link.addAttribute(StyleConstants.Underline, "none");
			link.addAttribute(StyleConstants.Foreground, Color.BLUE);
		}
		
		getViewport().add(editor);

		setPreferredSize(new Dimension(500, 470));		
        setMinimumSize(new Dimension(500, 470));		
	}
    
    public void setMainPaneFont(Font font) {
    	if (font == null) {
    		return;
    	}
    	
		HTMLDocument doc = (HTMLDocument) editor.getDocument();
		StyleSheet ss = doc.getStyleSheet();
		Style body = ss.getStyleSheets()[0].getStyle("body");
		if (body != null) {
			body.addAttribute(StyleConstants.FontFamily, font.getFamily());
			body.addAttribute(StyleConstants.FontSize, font.getSize() + "pt");
		}
    }

    public synchronized int showTimeLine(SuittarTimeLineViewBean timeLineViewBean) {

		SuittarTimeLineView view = SuittarTimeLineViewManager.getSuittarTimeLineView(timeLineViewBean);
		
		StringBuffer htmlContents = view.getContents();
    	clearDocument();
		editor.setText(htmlContents.toString());
		editor.setCaretPosition(0);

		return (timeLineViewBean.getStatusList().size() > 0) ? timeLineViewBean.getStatusList().get(0).getId() : -1;    	

    }
    
    public synchronized void showFollowing(UserWithStatus user,List<User> followings) {
    	showUsers(user, followings, true);
    }
    
    public synchronized void showFollowers(UserWithStatus user,List<User> followers) {
    	showUsers(user, followers, false);
    }
    
    private synchronized void showUsers(UserWithStatus user,List<User> users,boolean isFollowing) {
		StringBuffer htmlContents = SuittarUsersView.getContents(user,users,isFollowing);
    	clearDocument();
		editor.setText(htmlContents.toString());
		editor.setCaretPosition(0);    	    	
    }

    public synchronized void showDirectMessages(SuittarTimeLineViewBean timeLineViewBean,List<DirectMessage> sent,List<DirectMessage> receive) {
		StringBuffer htmlContents = SuittarDirectMessageView.getContents(timeLineViewBean,sent,receive);
    	clearDocument();
		editor.setText(htmlContents.toString());
		editor.setCaretPosition(0);    	
    }

	public void showAbout() {
		try {
			if (aboutURL == null) {
				aboutURL = SuittarResourceManager.getResourceUrl(SuittarResourceManager.HTML_ABOUT);
			}
			clearDocument();
			editor.setPage(aboutURL);
			editor.setCaretPosition(0);			
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void addSuittarHyperLinkListener(Suittar suittar) {
		editor.addHyperlinkListener(new SuittarHyperLinkListener(suittar));
	}
	
	public void clearDocument() {
		editor.setText("");
		Document doc = editor.getDocument();
		doc.putProperty(Document.StreamDescriptionProperty, null);
	}
		
}
